﻿//-----------------------------------------------------------------------
// <copyright file="SingletonDependncyTypeFinder.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: SingletonDependncyTypeFinder.cs
// * history : created by qinchaoyue 2017-10-20 06:05:59
// </copyright>
//-----------------------------------------------------------------------

using Slwy.FrameCore.Reflection;

namespace Slwy.FrameCore.Dependency
{
    using System;
    using System.Linq;

    public class SingletonDependncyTypeFinder : ITypeFinder
    {
        public SingletonDependncyTypeFinder()
        {
            this.AssemblyFinder = new DirectoryAssemblyFinder();
        }

        public IAllAssemblyFinder AssemblyFinder { get; set; }

        public Type[] Find(Func<Type, bool> predicate)
        {
            return this.FindAll().Where(predicate).ToArray();
        }

        public Type[] FindAll()
        {
            var assemblies = AssemblyFinder.FindAll();
            return assemblies.SelectMany(assembly =>
                    assembly.GetTypes().Where(type => typeof(ISingletonDependency).IsAssignableFrom(type) && !type.IsAbstract))
                .Distinct().ToArray();
        }
    }
}